/**
 * Created by m on 2018/12/25.
 */
$(function () {
//    加载二维码
    function randomCode() {
        var path = ip+"/Back/Admin/VerifyCode?tm=" + Math.random();
        $(".codeImg img").attr('src',path);
    }
//    刷新二维码
    $(".codeImg").click(function () {
        randomCode();
    });
//    登录
    $("#LoginBtn").click(function () {
        var _loginName = $("#loginName").val();
        var _password = $("#password").val();
        var _checkCode = $("#checkCode").val();

        $.ajax({
            type:"post",
            url:ip +'/Back/Admin/Login',
            headers:{"Content-type":"application/json","Accept":"application/json;charset=utf-8"},
            data:JSON.stringify({
                "checkCode": _checkCode,
                "loginName": _loginName,
                "password": hex_md5(_password + salt)
            }),
            xhrFields:{withCredentials:true},
            dataType:'json',
            success:function (data) {
                if(data.code == 1){
                    var token1 = data.data.token;
                    var _token = encodeURIComponent(token1);
                    SetCookie("loginName",_loginName);
                    SetCookie("token",_token);
                    data.code == "1" ? window.location.href = "index.html?loginNme=" + _loginName + "%token=" + _token : alert(data.message);$("#password").val("");$("checkCode").val()
                } else {
                  alert(data.message);
                  var path = ip + "/Back/Admin/VerifyCode?tm=" + Math.random();
                  $(".codeImg img").attr('src',path);
                }

            },
            error:function () {
                alert('登录出错');
            }

        });
    });
//    回车键登录
    $(document).keydown(function (event) {
        if(event.keyCode == 13){
            $("#LoginBtn").click();
        }
    });
});
